<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/6 0006
 * Time: 17:11
 */
namespace app\modules\content\models;

use app\libs\Method;
use yii\db\ActiveRecord;
use yii;

class Classes extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%classes}}';
    }

    public function rules()
    {
        return [
            // username and password are both required
            [['student', 'cate', '', 'duration'], 'required'],

        ];
    }

    public static function classData(){
        $data['classType'] = [
            1=>'小班课',
            2=>'大班课',
            3=>'一对一课',
        ];
        $data['courseType'] = [
            1=>'公开课',
            2=>'小班课',
            3=>'单项课',
            4=>'录播课',
            5=>'直播课',
            6=>'一对一课',
            7=>'封闭班',
            8=>'热门课程',
            9=>'封闭班',
        ];
        return $data;
    }

    public static function courseCategory(){
        $data = [
            1=>['id'=>1,'name'=>'经典课程','type'=>1],
            2=>['id'=>2,'name'=>'VIP直达','type'=>1],
            3=>['id'=>3,'name'=>'面授课','type'=>1],
            4=>['id'=>4,'name'=>'精品小课','type'=>1],
            5=>['id'=>5,'name'=>'真题解析','type'=>2],
            6=>['id'=>6,'name'=>'高分技巧','type'=>2],
            7=>['id'=>7,'name'=>'雅思词汇','type'=>2],
            8=>['id'=>8,'name'=>'雅思语法','type'=>2],
        ];
        return $data;
    }

    public static function getOrderInfo($data,$type=1){
        if (!empty($data['data'])) {
            foreach ($data['data'] as $key => $value) {
                if ($value['orderBelong'] == 12) {
                    $data['data'][$key]['image'] = $value['image']?Yii::$app->params['thinkUrl'].$value['image']:'';
                    if($type==1){
                        $data['data'][$key]['video'] = [];
                    }
                } elseif ($value['orderBelong'] == 8) {
                    if($type==1){
                        $data['data'][$key] = json_decode(Method::curl_post_fix(Yii::$app->params['ieltsUrl']."/order/api/class-detail",['id'=>$value['id']]),true);
                    }
                    $data['data'][$key]['image'] = Yii::$app->params['ieltsUrl'].$value['image'];
                }
                if($value['order_form'] == 1){
                    $data['data'][$key]['endTime'] = $value['endTime'];
                    $data['data'][$key]['expireTime'] = $value['endTime'];
                }else{
                    $data['data'][$key]['endTime'] = $value['endTime2'];
                    $data['data'][$key]['expireTime'] = $value['endTime2'];
                }
                $data['data'][$key]['startTime'] = $value['payTime'];
                $data['data'][$key]['price'] = (int)$value['payable'];//ios临时修改
            }
        }
        return $data;
    }

    public static function details($data){
        $classData = self::classData();
        $data['classType'] = '';
        $data['courseType'] = $data['catName'];
        if($data['orderBelong']==12){
            $data['image'] = $data['image']?Yii::$app->params['thinkUrl'].$data['image']:'';
            $id = intval($data['contentId']);
            $classes = Classes::findOne($id);
            if($classes){
                $data['classType'] = isset($classData['classType'][$classes->class])?$classData['classType'][$classes->class]:'';
                $data['courseType'] = isset($classData['courseType'][$classes->cate])?$classData['courseType'][$classes->cate]:'';
            }
        }else{
            $data['image'] = Yii::$app->params['ieltsUrl'].$data['image'];
        }
        return $data;
    }

    public static function courseList($type,$limit=100){
        $cateAll = Classes::courseCategory();
        $cate = $courses = [];
        foreach ($cateAll as $k=>$v){
            if($v['type']==$type){
                $cate[] = $v;
            }
        }
        foreach ($cate as $k=>$v){
            $courses[$k] = $v;
            $course = Classes::find()->where(['classesTag'=>$v['id'],'showType'=>$type,'classesPort'=>2])->orderBy('sort asc,id desc')->limit($limit)->asArray()->all();
            foreach ($course as $k2=>$v2){
                $course[$k2]['course_tag'] = !empty($v2['course_tag'])?explode(',',$v2['course_tag']):null;
                $course[$k2]['pic'] = $v2['pic']?Yii::$app->params['thinkUrl'].$v2['pic']:'';
                $course[$k2]['course_image'] = $v2['course_image']?Yii::$app->params['thinkUrl'].$v2['course_image']:'';
                if($type==2){
                    $course[$k2]['classesSdk'] = ClassesSdk::getClassSdk($v2['id']);
                    //老师
                    $teacher = null;
                    if($v2['systemTeacher']){
                        $teacher = Teachers::find()->where("id in ({$v2['systemTeacher']})")->asArray()->one();
                    }
                    if(isset($teacher)){
                        $course[$k2]['teacherName'] = $teacher['name'];
                        $course[$k2]['teacherPic'] = $teacher['pic']?Yii::$app->params['thinkUrl'].$teacher['pic']:Yii::$app->params['defaultImg'];
                    }else{
                        $course[$k2]['teacherName'] = '';
                        $course[$k2]['teacherPic'] = Yii::$app->params['defaultImg'];
                    }
                }
            }
            $courses[$k]['course'] = $course;
        }
        return ['cate'=>$cate,'courses'=>$courses];
    }

    public static function getCourse($classesTag,$type,$limit=3){
        $course = Classes::find()->where(['classesTag'=>$classesTag,'showType'=>$type,'classesPort'=>2])->orderBy('id desc')->limit($limit)->asArray()->all();
        foreach ($course as $k=>$v){
            $course[$k]['course_tag'] = !empty($v['course_tag'])?explode(',',$v['course_tag']):null;
            if($v['systemTeacher']){
                $teacher = Teachers::find()->where("id in ({$v['systemTeacher']})")->asArray()->one();
            }
            if(isset($teacher)){
                $course[$k]['teacherName'] = $teacher['name'];
                $course[$k]['teacherPic'] = $teacher['pic']?Yii::$app->params['thinkUrl'].$teacher['pic']:Yii::$app->params['defaultImg'];
            }else{
                $course[$k]['teacherName'] = '';
                $course[$k]['teacherPic'] = Yii::$app->params['defaultImg'];
            }
        }
        return $course;
    }
}